package com.anhuanjia.tools.eao.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoDataBill;
import com.anhuanjia.tools.eao.bean.EaoProjectDoc;
import com.anhuanjia.tools.eao.bean.EaoProjectInfo;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.collection.Mapx;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 项目存档接口
 * 
 * @author Administrator
 *
 */
@Alias(value = "projectArchive")
public class ProjectArchiveUI extends UIFacade {
	/**
	 * 根据项目id展示存档页面所有数据
	 */
	@Priv(loginType = LoginType.Member)
	public void showAll() {
		String pid = $V("ProjectId");
		if(StringUtils.isEmpty(pid)) {
			f("操作失败");
		}
		DataTable dt1 = new Q(
				"select Id,ProjectNumber,ProjectName,BuildUnit,BuildAddress,ReportType,PerjectLeader,ApprovalUnit,SurveyDate,AuditDate,BatchDate,AppraiseDate,DocumentStorageLocation,ArchivistsOpinion from eao_project_info where Id = ?",
				pid).fetch();
		String perject_leader = ""; // 项目负责人
		for (DataRow dr1 : dt1) {
			perject_leader = dr1.getString("PerjectLeader");
			DataTable dt2 = new Q("select FullName from Mh_Resource where Member = ? ", perject_leader).fetch();
			for (DataRow dr2 : dt2) {
				perject_leader = dr2.getString("FullName");
			}
		}
		DataTable dt2 = new Q(
				"select ProjectContract,ProjectTrustInstrument,Surver,ProjectStatusMap,AuditData,ProjectBatchData,AssessSign,StandardExecution,ReportOriginal,BuildPublicPromise,PublicPrintscreenUrl,PublicCens,PublicOpinion,PublicSpecialReports,HearingData,ForumData,BuildPublicText,BuildOtherData,ReportAuditor,ReportBatch from eao_project_doc where ProjectId = ?",
				pid).fetch();

		DataTable dt3 = new Q("select * from eao_data_bill where ProjectId =? order by ProjectDataName", pid).fetch();

		DataTable dt4 = new Q(
				"select GROUP_CONCAT(StaffsName) as StaffsName from(select  ProjectId,FullName as StaffsName from eao_project_staff as staff LEFT JOIN Mh_Resource on staff.StaffName=Mh_Resource.Member)as cy where ProjectId = ? GROUP BY ProjectId",
				pid).fetch();
		String staffs = ""; // dt4.getString(0, 0); // 项目成员
		for (DataRow dr4 : dt4) {
			staffs = dr4.getString(0);
		}

		$S("data1", dt1); // 项目详情
		this.Response.put("PerjectLeader", perject_leader);// 项目负责人
		this.Response.put("staffs", staffs);// 项目组成员
		Map<String, Map<String, Map<String, String>>> types = new HashMap<String, Map<String, Map<String, String>>>();
		for (int i = 0; i < 21; i++) {
			String is = i + "";
			Map<String, Map<String, String>> type = new HashMap<String, Map<String, String>>();
			for (DataRow dr3 : dt3) {
				Map<String, String> ent = new HashMap<String, String>();
				String name = dr3.getString("ProjectDataName");
				String Id = dr3.getString("Id");
				String Data_stock = dr3.getString("DataStock");
				ent.put("Id", Id);
				ent.put("DataStock", Data_stock);
				if (is.equals(name)) {
					type.put("B", ent);
				}
			}
			String value1 = "";
			for (DataRow dr2 : dt2) {
				Map<String, String> ent = new HashMap<String, String>();
				if (i != 20) {
					value1 = dr2.getString(i);
					ent.put("Url", value1);
					type.put("A", ent);
				} else {
					for (DataRow dr1 : dt1) {
						value1 = dr1.getString("DocumentStorageLocation");
					}
					ent.put("Url", value1);
					type.put("A", ent);
				}
			}
			types.put(is, type);
		}
		this.Response.put("bigs", types);// 项目文件数量

	}

	/**
	 * 添加或修改纸质文件数量
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdateStock(List<Mapx<String, Object>> stockLists) {
		Transaction transaction = new Transaction();
		if (stockLists != null) {
			for (Map<String, Object> list : stockLists) {
				System.out.println(list);
				EaoDataBill dataBill = new EaoDataBill();
				dataBill.setValue(list);

				String id = dataBill.getId();
				if (StringUtils.isEmpty(id)) {
					// 添加
					dataBill.setId(UUID.randomUUID().toString());
					transaction.insert(dataBill);
				} else {
					// 更新

					DataTable dt = new Q(" select * from EaoDataBill where Id = ?", id).fetch();
					EaoDataBill entity = new EaoDataBill();
					for (DataRow dr : dt) {
						entity.setValue(dr);
					}
					entity.setValue(dt.get(0));
					entity.setValue(list);
					transaction.update(entity);
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		}

	}

	/**
	 * 保存数据
	 * 
	 * @param bigs
	 */
	@Priv(loginType = LoginType.Member)
	public void SaveAll(String bigs) {
		String pid = $V("ProjectId");
		if (StringUtil.isNotEmpty(pid)) {
			Transaction transaction = new Transaction();

			List<Map<String, Map<String, Map<String, String>>>> list = JSON.parseObject(bigs,
					new TypeReference<List<Map<String, Map<String, Map<String, String>>>>>() {
					});
			DataTable dt2 = new Q("Select * from eao_project_doc where ProjectId = ?", pid).fetch();
			EaoProjectDoc projectDoc = new EaoProjectDoc();
			if (dt2.getRowCount() != 0) {
				for (DataRow dr2 : dt2) {
					projectDoc.setValue(dr2);
				}
			}
			for (Map<String, Map<String, Map<String, String>>> map : list) {
				for (int i = 0; i < 21; i++) {
					Map<String, Map<String, String>> map2 = map.get(i + "");
					String opinion = $V("ArchivistsOpinion");
					Map<String, String> mapA = map2.get("B");

					String Id = mapA.get("Id");
					String Data_stock = mapA.get("DataStock");

					DataTable dt = new Q(" select * from eao_data_bill where Id = ?", Id).fetch();
					EaoDataBill entity = new EaoDataBill();
					for (DataRow dr : dt) {
						entity.setValue(dr);
					}
					entity.setValue(dt.get(0));
					entity.setDataStock(Data_stock);
					transaction.update(entity);

					if (i != 20) {
						Map<String, String> mapB = map2.get("A");
						String Url = mapB.get("Url");
						switch (i) {
						case 0:
							projectDoc.setProjectContract(Url);

							break;
						case 1:
							projectDoc.setProjectTrustInstrument(Url);

							break;
						case 2:
							projectDoc.setSurver(Url);

							break;
						case 3:
							projectDoc.setProjectStatusMap(Url);

							break;
						case 4:
							projectDoc.setAuditData(Url);

							break;
						case 5:
							projectDoc.setProjectBatchData(Url);

							break;
						case 6:
							projectDoc.setAssessSign(Url);

							break;
						case 7:
							projectDoc.setStandardExecution(Url);

							break;
						case 8:
							projectDoc.setReportOriginal(Url);

							break;
						case 9:
							projectDoc.setBuildPublicPromise(Url);

							break;
						case 10:
							projectDoc.setPublicPrintscreenUrl(Url);

							break;
						case 11:
							projectDoc.setPublicCens(Url);

							break;
						case 12:
							projectDoc.setPublicOpinion(Url);

							break;
						case 13:
							projectDoc.setPublicSpecialReports(Url);

							break;
						case 14:
							projectDoc.setHearingData(Url);

							break;
						case 15:
							projectDoc.setForumData(Url);

							break;
						case 16:
							projectDoc.setBuildPublicText(Url);

							break;
						case 17:
							projectDoc.setBuildOtherData(Url);

							break;
						case 18:
							projectDoc.setReportAuditor(Url);

							break;
						case 19:
							projectDoc.setReportBatch(Url);

							break;

						default:
							break;
						}
					} else {
						Map<String, String> mapB = map2.get("A");
						String Url = mapB.get("Url");
						DataTable dt3 = new Q("select * from eao_project_info where Id = ?", pid).fetch();
						EaoProjectInfo projectInfo = new EaoProjectInfo();
						for (DataRow dr3 : dt3) {
							projectInfo.setValue(dr3);
						}
						projectInfo.setArchivistsOpinion(opinion);
						projectInfo.setDocumentStorageLocation(Url);
						transaction.update(projectInfo);
					}

				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		}
	}
}
